package game.module.qzmj.processor;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.ErrorCodeConstants;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.hall.logic.CheatManager;
import game.module.qzmj.bean.QzmjRoom;
import game.module.qzmj.logic.QzmjDataManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageQzmj.C2SQzmjJoinRoomCheck;
import lua.LuaMessageQzmj.S2CQzmjJoinRoomCheck;

@MsgCodeAnn(msgcode = C2SQzmjJoinRoomCheck.id, accessLimit = 500)
public class QzmjJoinRoomCheckProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(QzmjJoinRoomCheckProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		// TODO Auto-generated method stub
		C2SQzmjJoinRoomCheck reqMsg = C2SQzmjJoinRoomCheck.parse(request);
		String room_id = reqMsg.room_id;
		int playerId = hero.getId();
		logger.info("qz mj join room check,playeyId={},room_id={}", playerId, room_id);

		// 是否在房间中
		boolean isInRoom = QzmjDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (!QzmjDataManager.getInstance().checkRoomExist(room_id)) {
			S2ErrorCode respMsg = new S2ErrorCode(104);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		final QzmjRoom mjRoom = QzmjDataManager.getInstance().getMjRoom(room_id);
		int fangka = hero.getPlayerBean().getFang_ka();
		int fangka_cost = mjRoom.getFee();
		if (mjRoom.getCost_type() == 2 && fangka < fangka_cost)// 赢家收费
		{
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (mjRoom.getCost_type() == 1 && fangka < fangka_cost / 4) {// AA
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (CheatManager.getInstance().checkCheatAndPush(mjRoom.getPrevent_cheat(), mjRoom.getIps(), hero)) {
			return;
		}
		// 人数已满
		if (mjRoom.getPlayerList().size() >= 4) {
			S2ErrorCode respMsg = new S2ErrorCode(105);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		S2CQzmjJoinRoomCheck respMsg = new S2CQzmjJoinRoomCheck();

		hero.writeAndFlush(respMsg.build(hero.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	public String getIpAddr(String ipAddrRaw) {
		String retStr = ipAddrRaw;
		int fenIndex = ipAddrRaw.indexOf(":");
		if (fenIndex >= 0) {
			retStr = retStr.substring(0, fenIndex);
		}
		if (retStr.startsWith("/")) {
			retStr = retStr.substring(1);
		}
		return retStr;
	}

}
